<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <!-- <el-input v-model="searchModel.username" placeholder="用户名" clearable />
          <el-input v-model="searchModel.phone" placeholder="电话" clearable /> -->
          <el-button type="primary" @click="getDepList" round icon="el-icon-search">查询</el-button>
        </el-col>
        <el-col v-if="isSuper===1" :span="4" align="right">
          <el-button @click="editDep(null)" type="primary" icon="el-icon-plus" circle></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 查询结果栏 -->
    <el-card>
      <el-table :data="danwei_list" border style="width: 100%">
        <el-table-column label="#" width="100" align="center">
          <template slot-scope="scope">
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="idc_name" label="机房"  align="center">
        </el-table-column>
        <el-table-column prop="is_active" label="考试权限"  align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_active == 1">开通</el-tag>
            <el-tag v-else type="danger">关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="isSuper === 1" label="编辑" align="center">
          <template slot-scope="scope">
            <el-button @click="editDep(scope.row.id)" type="primary" icon="el-icon-edit" circle size="mini"></el-button>
            <el-button @click="open(scope.row)" type="danger" icon="el-icon-delete" circle size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo" :page-sizes="[5, 10, 50, 100]" :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible">
      <!-- <el-dialog :title="title" :visible.sync="dialogFormVisible"> -->
      <el-form :model="depForm">
        <el-form-item label="机房名称：" prop="idc_name" v-if="depForm.id == null || depForm.id == undefined"
          :label-width="formLabelWidth">
          <el-input v-model="depForm.idc_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="机房名称：" prop="idc_name" v-else :label-width="formLabelWidth">
          <el-input v-model="depForm.idc_name" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="考试权限：" :label-width="formLabelWidth">
          <el-switch v-model="depForm.is_active" :active-value="1" :inactive-value="0">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="addDep" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import userApi from "@/api/userManage";
import { mapGetters } from 'vuex';
export default {
  data() {
    return {
      depForm: {
        // "idc_name": "",
        // "is_active": 0,
      },
      formLabelWidth: "130px",
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      userList: [],
      danwei_list: [],
    };
  },
  methods: {
    open(dep) {
      this.$confirm(`您是否确认删除机房:${dep.idc_name}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          userApi.deleteDepById(dep.id).then((response) => {
            this.$message({
              type: "success",
              message: response.message,
            });
            this.getDepList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addDep() {
      userApi.saveDep(this.depForm).then((reponse) => {
        // 关闭弹窗
        this.dialogFormVisible = false;
        // 提示提交成功
        this.$message({
          message: reponse.message,
          type: "success",
        });
        // 刷新页面
        this.getDepList();
      });
    },
    clearForm() {
      this.depForm = {
        // "idc_name": "",
        // "is_active": 0,
      };
    },
    editDep(id) {
      if (id == null) {
        this.title = "新增机房";
      } else {
        this.title = "修改机房";
        //先从后端获取user信息
        userApi.getDepById(id).then((reponse) => {
          this.depForm = reponse.data;
        });
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getDepList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getDepList();
    },
    getDepList() {
      userApi.getDepList(this.searchModel).then((response) => {
        this.total = response.data.total;
        this.danwei_list = response.data.api_dep_list;
      });
    },
  },
  created() {
    this.getDepList();
  },
  computed: {
    ...mapGetters([
      // 'name',
      'isSuper',
      // 'nick_name'
    ]),
  }
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 88%;
}
</style>